﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PedesTrainProject.Properties;
using PedesTrainProject.Data;

namespace PedesTrainProject.GUI
{
    public partial class SimulationCreator : Form
    {
        private enum Tools { straight, crossing, corner, lights, inFlow, none, Tcrossing, background, pedestrianLights, run};
        private Grid.Directions direction = Grid.Directions.None;
        private Tools tools= Tools.none;
        private Report report = new Report();
        private Sections sections;
        private Bitmap drawArea;
        delegate void updateScreenCallback(System.Drawing.Drawing2D.GraphicsPath path);
        delegate void updateTimeCallback(DateTime currentTime);

        public Bitmap DrawArea
        {
            get { return drawArea; }
            set { drawArea = value; }
        }
        private int mousetraceX;
        private bool isRun = false;
        private Panel panel3 = new Panel();

        public int MousetraceX
        {
            get { return mousetraceX; }
            set { mousetraceX = value; }
        }
        private int mousetraceY;

        public int MousetraceY
        {
            get { return mousetraceY; }
            set { mousetraceY = value; }
        }
        private Bitmap drawTile = new Bitmap(100,100);
        private Bitmap backgroundTile = new Bitmap(100, 100);
        private AdjustCars adjustCars;
        private AdjustPedestrians adjustPedestrians;
        private Cross_Light_Setting lights;
        private File_settings fileSettings;
        private Core.ICoreGUI coreGUI;
        private Core.ICoreGUIRun coreGUIRun;
        private string tileName="none";
        private string pedestrians;
        private System.Drawing.Drawing2D.GraphicsPath tPath;
        private bool saved;
        private List<Data.Statistics> statistcs;
 
        public Core.ICoreGUI CoreGUI
        {
            get { return coreGUI; }
            set { coreGUI = value; }
        }
        private Bitmap resizeTile;

   //     private Grid.Tile tmpTile;
        
        public SimulationCreator(Core.ICoreGUI _coreGUI)
        {
            InitializeComponent();
            coreGUI = _coreGUI;
            sections = new Sections(report);
            
            drawArea = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            pictureBox1.Image = drawArea;
            backgroundTile = Resources.grass;
            for (int x = 0; x < 4; x++)
             {
                 for (int y = 0; y < 4; y++)
                 {
                     Graphics.FromImage(pictureBox1.Image).DrawImage(backgroundTile, 100*x, 100*y, 100, 100);
                 }
             }

            Pen pen1 = new Pen(Color.Black);

            for (int i = 1; i < 4; i++)
            {
                Graphics.FromImage(pictureBox1.Image).DrawLine(pen1, i * pictureBox1.Width / 4, 0, i * pictureBox1.Width / 4, pictureBox1.Height);
            }
            for (int i = 1; i < 4; i++)
            {
                Graphics.FromImage(pictureBox1.Image).DrawLine(pen1, 0, i * pictureBox1.Height / 4, pictureBox1.Width, i * pictureBox1.Height / 4);
            }

            adjustCars = new AdjustCars(this, new System.Windows.Vector(mousetraceX, mousetraceY));
            adjustPedestrians = new AdjustPedestrians(this, new System.Windows.Vector(mousetraceX, mousetraceY));
            backgroundTile = Resources.pavement;

            statistcs = new List<Statistics>();
        }

        private void toolStripProgressBar1_Click(object sender, EventArgs e)
        {

        }

        private void newSimulationToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void lightTimingsToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void previewReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            sections.Show();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void exitSimulationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void drawLight(PaintEventArgs e)
        {

        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (isRun == false)
            {
                Pen pen1 = new Pen(Color.Black);
                Pen red = new Pen(Color.Red, 3);
                Pen green = new Pen(Color.Green, 3);
                Pen yellow = new Pen(Color.Yellow, 3);
                Brush greenBrush = new SolidBrush(Color.Green);
                Brush redBrush = new SolidBrush(Color.Red);
                Brush yellowBrush = new SolidBrush(Color.Yellow);
                Brush blackBrush = new SolidBrush(Color.Black);

                for (int i = 1; i < 4; i++)
                {
                    e.Graphics.DrawLine(pen1, i * pictureBox1.Width / 4, 0, i * pictureBox1.Width / 4, pictureBox1.Height);
                }
                for (int i = 1; i < 4; i++)
                {
                    e.Graphics.DrawLine(pen1, 0, i * pictureBox1.Height / 4, pictureBox1.Width, i * pictureBox1.Height / 4);
                }

                if (tools == Tools.crossing || tools == Tools.straight || tools == Tools.Tcrossing || tools == Tools.corner)
                {
                    int x, y;
                    x = mousetraceX;
                    y = mousetraceY;

                    x /= (pictureBox1.Width / 4);
                    x *= (pictureBox1.Width / 4);
                    y /= (pictureBox1.Height / 4);
                    y *= (pictureBox1.Height / 4);

                    e.Graphics.DrawImage(backgroundTile, x, y, 100, 100);
                    e.Graphics.DrawImage(drawTile, x, y, 100, 100);

                    if (tools == Tools.Tcrossing || tools == Tools.crossing)
                    {

                        //left-up
                        e.Graphics.FillRectangle(blackBrush, x + 15, y + 2, 9, 20);
                        e.Graphics.DrawEllipse(green, x + 17, y + 4, 3, 3);
                        e.Graphics.DrawEllipse(yellow, x + 17, y + 10, 3, 3);
                        e.Graphics.DrawEllipse(red, x + 17, y + 16, 3, 3);
                        //right-down
                        int o = 62;
                        e.Graphics.FillRectangle(blackBrush, x + 15 + o, y + 2 + o + 12, 9, 20);
                        e.Graphics.DrawEllipse(green, x + 17 + o, y + 4 + o + 12, 3, 3);
                        e.Graphics.DrawEllipse(yellow, x + 17 + o, y + 10 + o + 12, 3, 3);
                        e.Graphics.DrawEllipse(red, x + 17 + o, y + 16 + o + 12, 3, 3);
                        //left-down
                        e.Graphics.FillRectangle(blackBrush, x + 3, y + 15 + o, 20, 9);
                        e.Graphics.DrawEllipse(green, x + 4, y + 17 + o, 3, 3);
                        e.Graphics.DrawEllipse(yellow, x + 10, y + 17 + o, 3, 3);
                        e.Graphics.DrawEllipse(red, x + 16, y + 17 + o, 3, 3);
                        if (tools == Tools.crossing)
                        {
                            //right-up
                            e.Graphics.FillRectangle(blackBrush, x + 14 + o, y + 15, 20, 9);
                            e.Graphics.DrawEllipse(green, x + 4 + o + 12, y + 17, 3, 3);
                            e.Graphics.DrawEllipse(yellow, x + 10 + o + 12, y + 17, 3, 3);
                            e.Graphics.DrawEllipse(red, x + 16 + o + 12, y + 17, 3, 3);
                        }
                    }

                    if (tools == Tools.straight)
                    {
                        int o = 62;
                        if (tileName == "ver")
                        {
                            //left-up
                            e.Graphics.FillRectangle(blackBrush, x + 15, y + 2, 9, 20);
                            e.Graphics.DrawEllipse(green, x + 17, y + 4, 3, 3);
                            e.Graphics.DrawEllipse(yellow, x + 17, y + 10, 3, 3);
                            e.Graphics.DrawEllipse(red, x + 17, y + 16, 3, 3);
                            //right-down

                            e.Graphics.FillRectangle(blackBrush, x + 15 + o, y + 2 + o + 12, 9, 20);
                            e.Graphics.DrawEllipse(green, x + 17 + o, y + 4 + o + 12, 3, 3);
                            e.Graphics.DrawEllipse(yellow, x + 17 + o, y + 10 + o + 12, 3, 3);
                            e.Graphics.DrawEllipse(red, x + 17 + o, y + 16 + o + 12, 3, 3);
                        }
                        if (tileName == "hor")
                        {
                            //left-down
                            e.Graphics.FillRectangle(blackBrush, x + 3, y + 15 + o, 20, 9);
                            e.Graphics.DrawEllipse(green, x + 4, y + 17 + o, 3, 3);
                            e.Graphics.DrawEllipse(yellow, x + 10, y + 17 + o, 3, 3);
                            e.Graphics.DrawEllipse(red, x + 16, y + 17 + o, 3, 3);

                            //right-up
                            e.Graphics.FillRectangle(blackBrush, x + 14 + o, y + 15, 20, 9);
                            e.Graphics.DrawEllipse(green, x + 4 + o + 12, y + 17, 3, 3);
                            e.Graphics.DrawEllipse(yellow, x + 10 + o + 12, y + 17, 3, 3);
                            e.Graphics.DrawEllipse(red, x + 16 + o + 12, y + 17, 3, 3);
                        }
                    }


                }
                if (tools == Tools.background)
                {
                    int x, y;
                    x = mousetraceX;
                    y = mousetraceY;

                    x /= (pictureBox1.Width / 4);
                    x *= (pictureBox1.Width / 4);
                    y /= (pictureBox1.Height / 4);
                    y *= (pictureBox1.Height / 4);

                    e.Graphics.DrawImage(backgroundTile, x, y, 100, 100);
                }

                if (tools == Tools.inFlow)
                {
                    e.Graphics.DrawImage(drawTile, mousetraceX - 10, mousetraceY - 10);
                }
            }
            if (isRun == true)
            {
                lock (this)
                {
                    Pen red = new Pen(Color.Silver, 6);
                    e.Graphics.DrawPath(red, tPath);
                }
            }
        }

        // straight
        private void button1_Click(object sender, EventArgs e)
        {
            tileName = "none";
            tools = Tools.straight;
            drawTile = Resources.straight_1_1;
            this.Cursor = Cursors.Hand;
            button5.Image = Resources.straight_1_1small;
            resizeTile = new Bitmap(Resources.straight_pedestrian_1_1, 50, 50);
            button6.Image = resizeTile;
            button7.Enabled = false;
            button8.Enabled = false;
            direction = Grid.Directions.East;
        }

        // crossing
        private void button3_Click(object sender, EventArgs e)
        {
            tileName = "none";
            button7.Enabled = true;
            button8.Enabled = true;
            tools = Tools.crossing;
            drawTile = Resources.X_crossing_1;
            this.Cursor = Cursors.Hand;
            toolStripStatusLabel4.Text = "Place a crossing on a grid";
            resizeTile = new Bitmap(Resources.X_crossing_1, 50, 50);
            button5.Image = resizeTile;
            resizeTile = new Bitmap(Resources.X_crossing_pedestrian_2, 50, 50);
            button6.Image = resizeTile;
            resizeTile = new Bitmap(Resources.X_crossing_pedestrian_2, 50, 50);
            resizeTile.RotateFlip(RotateFlipType.Rotate90FlipNone);
            button7.Image = resizeTile;
            resizeTile = new Bitmap(Resources.X_crossing_pedestrian_1, 50, 50);
            button8.Image = resizeTile;
            pictureBox1.Invalidate();
            direction = Grid.Directions.East;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
          //  if (e.Button == System.Windows.Forms.MouseButtons.None)
            if(isRun==false)
            {
                mousetraceX = e.X;
                mousetraceY = e.Y;
                /*
                toolStripStatusLabel5.Text ="" + mousetraceX;
                toolStripStatusLabel6.Text = "" + mousetraceY;
                toolStripStatusLabel7.Text = "" +Core.Calculations.calculateTileCoordinates(new System.Windows.Vector( mousetraceX,mousetraceY)).X;
                toolStripStatusLabel8.Text = "" + Core.Calculations.calculateTileCoordinates(new System.Windows.Vector(mousetraceX, mousetraceY)).Y;
                toolStripStatusLabel9.Text = "" + Core.Calculations.tileListPosition(Core.Calculations.calculateTileCoordinates(new System.Windows.Vector(mousetraceX, mousetraceY)));
               */
                 pictureBox1.Invalidate();
            }

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            tileName = "none";
            tools = Tools.corner;
            drawTile = Resources.L_crossing_1;
            toolStripStatusLabel4.Text = "Place a corner on a grid";
            resizeTile = new Bitmap(Resources.L_crossing_1,50,50);
            button5.Image = resizeTile;
            resizeTile = new Bitmap(Resources.L_crossing_pedestrian_1, 50, 50);
            button6.Image = resizeTile;
            button7.Enabled = false;
            button8.Enabled = false;
            direction = Grid.Directions.East;
        }
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right && (tools == Tools.crossing || tools == Tools.straight || tools == Tools.Tcrossing || tools == Tools.corner))
            {
                drawTile.RotateFlip(RotateFlipType.Rotate90FlipNone);
                if (direction == Grid.Directions.West) direction = Grid.Directions.North;
                else direction++;
                if (tileName == "ver") tileName = "hor"; 
                if (tileName == "hor") tileName = "ver";
            }
            if (e.Button == System.Windows.Forms.MouseButtons.Left && (tools == Tools.crossing || tools == Tools.straight || tools == Tools.Tcrossing || tools == Tools.corner))
            {
                int x, y;
                x = mousetraceX;
                y = mousetraceY;

                x /= (pictureBox1.Width / 4);
                x *= (pictureBox1.Width / 4);
                y /= (pictureBox1.Height / 4);
                y *= (pictureBox1.Height / 4);

                Graphics.FromImage(pictureBox1.Image).DrawImage(backgroundTile, x, y, 100, 100);
                Graphics.FromImage(pictureBox1.Image).DrawImage(drawTile, x, y,100,100);
                Pen pen1 = new Pen(Color.Black);
                Pen red = new Pen(Color.Red, 3);
                Pen green = new Pen(Color.Green, 3);
                Pen yellow = new Pen(Color.Yellow, 3);
                Brush greenBrush = new SolidBrush(Color.Green);
                Brush redBrush = new SolidBrush(Color.Red);
                Brush yellowBrush = new SolidBrush(Color.Yellow);
                Brush blackBrush = new SolidBrush(Color.Black);
                
                if (tools == Tools.crossing)
                {
                    coreGUI.addTile(coreGUI.createTile(coreGUI.createRoad("crossing", tileName, direction)), new System.Windows.Vector(mousetraceX, mousetraceY));
                    Graphics.FromImage(pictureBox1.Image).DrawImage(backgroundTile, x, y, 100, 100);
                    Graphics.FromImage(pictureBox1.Image).DrawImage(drawTile, x, y, 100, 100);
                    
                          
                        Graphics.FromImage(pictureBox1.Image).FillRectangle(blackBrush, x + 15, y + 2, 9, 20);
                        //left-up
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(green, x + 17, y + 4, 3, 3);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(yellow, x + 17, y + 10, 3, 3);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(red, x + 17, y + 16, 3, 3);
                        //right-down
                        int o = 62;
                        Graphics.FromImage(pictureBox1.Image).FillRectangle(blackBrush, x + 15 + o, y + 2 + o + 12, 9, 20);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(green, x + 17 + o, y + 4 + o + 12, 3, 3);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(yellow, x + 17 + o, y + 10 + o + 12, 3, 3);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(red, x + 17 + o, y + 16 + o + 12, 3, 3);
                        //left-down
                        Graphics.FromImage(pictureBox1.Image).FillRectangle(blackBrush, x + 3, y + 15 + o, 20, 9);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(green, x + 4, y + 17 + o, 3, 3);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(yellow, x + 10, y + 17 + o, 3, 3);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(red, x + 16, y + 17 + o, 3, 3);
                        //right-up
                        Graphics.FromImage(pictureBox1.Image).FillRectangle(blackBrush, x + 14 + o, y + 15, 20, 9);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(green, x + 4 + o + 12, y + 17, 3, 3);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(yellow, x + 10 + o + 12, y + 17, 3, 3);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(red, x + 16 + o + 12, y + 17, 3, 3);
                    
                }
                if (tools == Tools.straight)
                {
                    int o = 62;
                    if (tileName == "ver")
                    {
                        //left-up
                        Graphics.FromImage(pictureBox1.Image).FillRectangle(blackBrush, x + 15, y + 2, 9, 20);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(green, x + 17, y + 4, 3, 3);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(yellow, x + 17, y + 10, 3, 3);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(red, x + 17, y + 16, 3, 3);
                        //right-down

                        Graphics.FromImage(pictureBox1.Image).FillRectangle(blackBrush, x + 15 + o, y + 2 + o + 12, 9, 20);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(green, x + 17 + o, y + 4 + o + 12, 3, 3);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(yellow, x + 17 + o, y + 10 + o + 12, 3, 3);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(red, x + 17 + o, y + 16 + o + 12, 3, 3);
                    }
                    if (tileName == "hor")
                    {
                        //left-down
                        Graphics.FromImage(pictureBox1.Image).FillRectangle(blackBrush, x + 3, y + 15 + o, 20, 9);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(green, x + 4, y + 17 + o, 3, 3);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(yellow, x + 10, y + 17 + o, 3, 3);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(red, x + 16, y + 17 + o, 3, 3);

                        //right-up
                        Graphics.FromImage(pictureBox1.Image).FillRectangle(blackBrush, x + 14 + o, y + 15, 20, 9);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(green, x + 4 + o + 12, y + 17, 3, 3);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(yellow, x + 10 + o + 12, y + 17, 3, 3);
                        Graphics.FromImage(pictureBox1.Image).DrawEllipse(red, x + 16 + o + 12, y + 17, 3, 3);
                    }
                }
                if (tools == Tools.straight)
                {
                    coreGUI.addTile(coreGUI.createTile(coreGUI.createRoad("straight", tileName, direction)), new System.Windows.Vector(mousetraceX, mousetraceY));
                }
                if (tools == Tools.Tcrossing)
                {
                    coreGUI.addTile(coreGUI.createTile(coreGUI.createRoad("Tcrossing", tileName, direction)), new System.Windows.Vector(mousetraceX, mousetraceY));
                }
                if (tools == Tools.corner)
                {
                    coreGUI.addTile(coreGUI.createTile(coreGUI.createRoad("corner", tileName, direction)), new System.Windows.Vector(mousetraceX, mousetraceY));
                }
                
            }
            if (e.Button == System.Windows.Forms.MouseButtons.Left && tools == Tools.background)
            {
                int x, y;
                x = mousetraceX;
                y = mousetraceY;

                x /= (pictureBox1.Width / 4);
                x *= (pictureBox1.Width / 4);
                y /= (pictureBox1.Height / 4);
                y *= (pictureBox1.Height / 4);

                Graphics.FromImage(pictureBox1.Image).DrawImage(backgroundTile, x, y, 100, 100);
                
            }
            if (e.Button == System.Windows.Forms.MouseButtons.Left && tools == Tools.inFlow)
            {
               if(coreGUI.canSetInFlowFlag(new System.Windows.Vector(mousetraceX,mousetraceY)))
               {
                Graphics.FromImage(pictureBox1.Image).DrawImage(drawTile, mousetraceX-10, mousetraceY-10);
                pictureBox1.Invalidate();
                adjustCars = new AdjustCars(this, new System.Windows.Vector(mousetraceX, mousetraceY));
                adjustCars.Show();
                this.Enabled = false;
               }
            }
            if (e.Button == System.Windows.Forms.MouseButtons.Left && tools == Tools.lights)
            {
                if (coreGUI.crossingPresent(new System.Windows.Vector(mousetraceX,mousetraceY)))
                {
                this.Enabled = false;
                lights = new Cross_Light_Setting(this);
                lights.Show();
                }
            }
            if (e.Button == System.Windows.Forms.MouseButtons.Left && tools == Tools.pedestrianLights)
            {
                if (coreGUI.pedestrianCrossingPresent(new System.Windows.Vector(mousetraceX,mousetraceY)))
                {
                    this.Enabled = false;
                    lights = new Cross_Light_Setting(this);
                    lights.Show();
                }
            }
            if (e.Button == System.Windows.Forms.MouseButtons.Right && tools == Tools.lights || tools == Tools.none)
            {
                contextMenuStrip1.Show(Cursor.Position);
            }
            if (e.Button == System.Windows.Forms.MouseButtons.Right && tools == Tools.inFlow)
            {
                if(coreGUI.inFlowFlagPresent(new System.Windows.Vector(mousetraceX,mousetraceY)))
                contextMenuStrip2.Show(Cursor.Position);
            }
        }
        private void button12_Click(object sender, EventArgs e)
        {
            tools = Tools.inFlow;
            this.Cursor = Cursors.Hand;
            drawTile = Resources.flaga;
            toolStripStatusLabel4.Text = "Place an inflow flag on a a desired road";
        }
        private void button14_Click(object sender, EventArgs e)
        {
            adjustPedestrians.Show();
        }
        private void pickCrossingToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < drawArea.Width; i++)
            {
                for (int j = 0; j < drawArea.Height; j++)
                {
                    drawArea.SetPixel(i, j, System.Drawing.Color.White);
                }
            }
            pictureBox1.Invalidate();
        }
        private void button13_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
            tools = Tools.lights;
            toolStripStatusLabel4.Text = "Specify a crossing to alter light times";
            pictureBox1.Invalidate();
        }
        private void lightsToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            lights = new Cross_Light_Setting(this);
            lights.Show();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            tileName = "none";
            button7.Enabled = true;
            button8.Enabled = true;
            resizeTile = new Bitmap(Resources.T_crossing_1, 50, 50);
            tools = Tools.Tcrossing;
            drawTile = Resources.T_crossing_1;
            button5.Image = resizeTile;
            resizeTile = new Bitmap(Resources.T_crossing_pedestrian_1, 50, 50);
            button6.Image = resizeTile;
            resizeTile = new Bitmap(Resources.T_crossing_pedestrian_3, 50, 50);
            button7.Image = resizeTile;
            resizeTile = new Bitmap(Resources.T_crossing_pedestrian_7, 50, 50);
            button8.Image = resizeTile;
            direction = Grid.Directions.East;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            tileName = "both";
            if (tools == Tools.crossing) drawTile = Resources.X_crossing_pedestrian_1; 
            if (tools == Tools.Tcrossing) drawTile = Resources.T_crossing_pedestrian_7; 
            
            pictureBox1.Invalidate();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            tileName = "none";
            if (tools == Tools.crossing) drawTile = Resources.X_crossing_1;
            if (tools == Tools.crossing) drawTile = Resources.straight_1; 
            
            pictureBox1.Invalidate();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            tileName = "hor";
            if (tools == Tools.crossing) drawTile = Resources.X_crossing_pedestrian_2; 
            if (tools == Tools.straight) drawTile = Resources.straight_pedestrian_1_1; 
            if (tools == Tools.corner) drawTile = Resources.L_crossing_pedestrian_1; 
            if (tools == Tools.Tcrossing) drawTile = Resources.T_crossing_pedestrian_1; 
            pictureBox1.Invalidate();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (tools == Tools.crossing)
            {
                drawTile = Resources.X_crossing_pedestrian_2;
                drawTile.RotateFlip(RotateFlipType.Rotate90FlipNone);
                tileName = "ver";
            }
            if (tools == Tools.Tcrossing) drawTile = Resources.T_crossing_pedestrian_3;

            pictureBox1.Invalidate();
        }

        private void button15_Click(object sender, EventArgs e)
        {
            fileSettings = new File_settings(this);
            fileSettings.Show();
        }

        private void button11_Click(object sender, EventArgs e)
        {
            backgroundTile = Resources.pavement;
            button9.Image = Resources.pavement;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            backgroundTile = Resources.grass;
            button9.Image = Resources.grass;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            tools = Tools.background;
        }

        // Start Simulation
        private void button16_Click(object sender, EventArgs e)
        {
            panel2.Dispose();
            Panel panel3 = new Panel();
            toolStripStatusLabel4.Dispose();
            //TODO include statistics
            this.panel3.Location = new System.Drawing.Point(421, 36);
            this.panel3.Name = "panel3";
            this.panel3.Size = new System.Drawing.Size(194, 403);
            this.panel3.TabIndex = 13;
            this.Controls.Add(this.panel3);
            Graphics g = Graphics.FromImage(pictureBox1.Image);
            coreGUIRun=coreGUI.startSimulation(g, pictureBox1,drawArea,this);
            //coreGUIRun = coreGUI.
            tools = Tools.run;
            isRun = true;
            speedBar.Visible = true;
            speedBar.Enabled = true;
        }

        private void beginToolStripMenuItem_Click(object sender, EventArgs e)
        {

            /*panel2.Dispose();
            Panel panel3 = new Panel();
           
            //TODO include statistics
            this.panel3.Location = new System.Drawing.Point(421, 36);
            this.panel3.Name = "panel3";
            this.panel3.Size = new System.Drawing.Size(194, 403);
            this.panel3.TabIndex = 13;
            this.Controls.Add(this.panel3);
            Graphics g = Graphics.FromImage(pictureBox1.Image);
            coreGUI.startSimulation(g,pictureBox1,drawArea,this);
            //coreGUIRun = coreGUI.
            tools = Tools.run;
            isRun = true;
            */
            panel2.Dispose();
            Panel panel3 = new Panel();
            toolStripStatusLabel4.Dispose();
            //TODO include statistics
            this.panel3.Location = new System.Drawing.Point(421, 36);
            this.panel3.Name = "panel3";
            this.panel3.Size = new System.Drawing.Size(194, 403);
            this.panel3.TabIndex = 13;
            this.Controls.Add(this.panel3);
            Graphics g = Graphics.FromImage(pictureBox1.Image);
            coreGUIRun = coreGUI.startSimulation(g, pictureBox1, drawArea, this);
            //coreGUIRun = coreGUI.
            tools = Tools.run;
            isRun = true;
            speedBar.Visible = true;
            speedBar.Enabled = true;
        }

        public void updateScreen(System.Drawing.Drawing2D.GraphicsPath path)
        {
            if (this.pictureBox1.InvokeRequired)
            {
                updateScreenCallback d = new updateScreenCallback(updateScreen);
                this.Invoke(d,new object[] { path });
            }
            else
            {
                tPath = path;
                pictureBox1.Update();
                pictureBox1.Invalidate();
            }
        }

        public void updateTime(DateTime time)
        {
            toolStripStatusLabel5.Text = "Day: " + time.Day.ToString() + " Hour: " + time.Hour.ToString() + " Minute: " + time.Minute.ToString() + " Second: " + time.Second.ToString();
        }

        private void SimulationCreator_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(isRun) coreGUIRun.stop();
        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            coreGUIRun.stop();
            Data.Statistics s = coreGUIRun.getStatistic();
            //MessageBox.Show("Time: " + s.StartTime);
            statistcs.Add(s);
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (coreGUI.inFlowFlagPresent(new System.Windows.Vector(mousetraceX, mousetraceY)))
            {
                Graphics.FromImage(pictureBox1.Image).DrawImage(drawTile, mousetraceX - 10, mousetraceY - 10);
                pictureBox1.Invalidate();
                adjustCars = new AdjustCars(this, new System.Windows.Vector(mousetraceX, mousetraceY));
                adjustCars.Show();
                this.Enabled = false;
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveGridFileDialog = new SaveFileDialog();
            if (saveGridFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (saveGridFileDialog.FileName != "" && saveGridFileDialog.FileName != null)
                {
                    if (coreGUI.SaveGridLayout(saveGridFileDialog.FileName))
                    {
                        MessageBox.Show("Layout Grid has been successfully saved");
                    }
                    else
                    {
                        MessageBox.Show("Save Failed");
                    }
                }
            }
        }

        private void loadToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            openGridFileDialog = new OpenFileDialog();
            openGridFileDialog.FileName = "";

            if (openGridFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (openGridFileDialog.FileName != null && openGridFileDialog.FileName != "")
                {
                    if (coreGUI.LoadGridLayout(openGridFileDialog.FileName))
                    {
                        MessageBox.Show("Load Success");
                        List<Grid.Tile> tiles = coreGUI.GetTiles();
                        /*
                        string result = "";

                        for (int i = 0; i < 4; i++)
                            result += "[" + tiles[i].Road.Name + "," + tiles[i].Road.Skin + "] ";
                        result += "\n";

                        for (int i = 4; i < 8; i++)
                            result += "[" + tiles[i].Road.Name + "," + tiles[i].Road.Skin + "] ";
                        result += "\n";

                        for (int i = 8; i < 12; i++)
                            result += "[" + tiles[i].Road.Name + "," + tiles[i].Road.Skin + "] ";
                        result += "\n";

                        for (int i = 12; i < 16; i++)
                            result += "[" + tiles[i].Road.Name + "," + tiles[i].Road.Skin + "] ";
                        MessageBox.Show(result, "Loaded Grid Layout");
                         * */
                        drawLayout();
                    }
                    else
                    {
                        MessageBox.Show("Load Failed");
                    }
                }
            }
        }

        public void drawLayout()
        {
            Bitmap rotationTemp = new Bitmap(100,100);
            int count = 0;
            foreach(Grid.Tile tile in coreGUI.GetTiles())
            {
                Graphics.FromImage(pictureBox1.Image).DrawImage(Resources.pavement, (int)Core.Calculations.gridPosition(count).X * 100, (int)Core.Calculations.gridPosition(count).Y * 100, 100, 100);
                if (tile.Road.Name == "crossing")
                {

                    if (tile.Road.Skin == "none") rotationTemp = Resources.X_crossing_1;
                    if (tile.Road.Skin == "both") rotationTemp = Resources.X_crossing_1both;
                    if (tile.Road.Skin == "hor")  rotationTemp = Resources.X_crossing_1hor;
                    if (tile.Road.Skin == "ver")  rotationTemp = Resources.X_crossing_1ver;
                }
                if (tile.Road.Name == "Tcrossing")
                {
                    rotationTemp = Resources.T_crossing_1;
                }
                if (tile.Road.Name == "corner")
                {
                    rotationTemp = Resources.L_crossing_1;
                }
                if (tile.Road.Name == "straight")
                {
                    if(tile.Road.Skin == "none") rotationTemp = Resources.straight_1_1;
                    if (tile.Road.Skin != "none") rotationTemp = Resources.straight_pedestrian_1_1;

                    if (tile.Road.Lanes[0].Direction == Grid.Directions.North || tile.Road.Lanes[0].Direction == Grid.Directions.South)
                    {
                        rotationTemp = Resources.straight_1_1;
                        rotationTemp.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    }
                }
                if(tile.Road.Name!="empty") Graphics.FromImage(pictureBox1.Image).DrawImage(rotationTemp, (int)Core.Calculations.gridPosition(count).X * 100, (int)Core.Calculations.gridPosition(count).Y * 100, 100, 100);
                count++;
            }
        }

        private void SimulationCreator_Load(object sender, EventArgs e)
        {

        }

        private void speedBar_Scroll(object sender, EventArgs e)
        {
            coreGUIRun.setSpeed(speedBar.Value);
        }

        private void viewStatisticToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Current_Statistic(statistcs).Show();
        }

    }
}
